package com.ymh.util;

import com.ymh.common.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

/**
 * @author: 13590
 * @date: 2020/11/10 19:17
 * @description:
 */

@Slf4j
public class ResponseUtils {

  public static <T> Mono<Void> write(ServerHttpResponse response, T t) {
    response.setStatusCode(HttpStatus.UNAUTHORIZED);
    response.getHeaders().add("Content-Type", "application/json; charset=UTF-8");
    byte[] dataBytes = JSONUtil.toJSON(t).getBytes();
    DataBuffer bodyDataBuffer = response.bufferFactory().wrap(dataBytes);
    return response.writeWith(Mono.just(bodyDataBuffer));
  }
}
